package org.example.jdbc.bank.dao;

import org.example.jdbc.bank.bean.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/4/23
 * Email haijunzhou@hnit.edu.cn
 */
@Repository
public class AccoutDaoImpl implements AccountDao{

    @Resource
    JdbcTemplate jdbcTemplate;


    @Override
    public void insert(Account t) {
        jdbcTemplate.update("insert into bank_account values(?,?,?)",
                t.getId(),t.getName(),t.getMoney());
    }

    @Override
    public void update(Account t) {
        jdbcTemplate.update("update bank_account set password=?,balance=? where id=?",
                t.getName(),t.getMoney(),t.getId());
    }

    @Override
    public Account selectById(Integer id) {
        Account account = jdbcTemplate.queryForObject("select * from bank_account where id=?",
                new RowMapper<Account>() {
                    @Override
                    public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Account account = new Account();
                        account.setId(rs.getInt("id"));
                        account.setName(rs.getString("password"));
                        account.setMoney(rs.getDouble("balance"));
                        return account;
                    }
                }, id);
        return account;
    }
}
